// pages/msg_notice_add/msg_notice_add.js
let app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        array1: [{
                'LNAME': '单股'
            },
            {
                'LNAME': '双股'
            },
            {
                'LNAME': '三股'
            },
        ],
        my_table: [],
        LCURRENCYID: '人民币',
        LNDEN: '',
        LPTNCODE: '',
        LITEMCODE: '',
        LITEMSTD: '',
        LITEMCOMPOS: '',
        LCOMPANYID: '',
        LSALEOPID: '',
        LNIANDU: '',
        LWEIGHT: '',
        LREMARK: '',
        LUNIT: '千克',
        LNUMBER: '',
        LSAP:'',
    },
    // ListTouch触摸开始
    ListTouchStart(e) {
        this.setData({
            ListTouchStart: e.touches[0].pageX
        })
    },
    // ListTouch计算方向
    ListTouchMove(e) {
        this.setData({
            ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
        })
    },
    // ListTouch计算滚动
    ListTouchEnd(e) {
        if (this.data.ListTouchDirection == 'left') {
            this.setData({
                modalName: e.currentTarget.dataset.target
            })
        } else {
            this.setData({
                modalName: null
            })
        }
        this.setData({
            ListTouchDirection: null
        })
    },
    contract_add() {
        wx.navigateTo({
            url: '../msg_notice_add_details/msg_notice_add_details?list=' + '1'
        })
    },
    bindPickerChange1(e) {
        this.setData({
            LREQDATE: e.detail.value
        })
    },
    bindPickerChange2(e) {
        this.setData({
            index1: e.detail.value,
        })
        for (var i = 0; i < this.data.array1.length; i++) {
            if (i == e.detail.value) {
                this.setData({
                    LNUMBER: this.data.array1[i].LNAME
                })
            }
        }
    },
    bindinputip(e) {
        if (e.currentTarget.dataset.id == '1') {
            this.data.LPTNCODE = e.detail.value
        } else if (e.currentTarget.dataset.id == '2') {
            this.data.LCOMPANYID = e.detail.value
        } else if (e.currentTarget.dataset.id == '3') {
            this.data.LSALEOPID = e.detail.value
        } else if (e.currentTarget.dataset.id == '4') {
            this.data.LITEMCODE = e.detail.value
        } else if (e.currentTarget.dataset.id == '5') {
            this.data.LITEMNAME = e.detail.value
        } else if (e.currentTarget.dataset.id == '6') {
            this.data.LITEMSTD = e.detail.value
        } else if (e.currentTarget.dataset.id == '7') {
            this.data.LITEMCOMPOS = e.detail.value
        } else if (e.currentTarget.dataset.id == '8') {
            this.data.LNIANDU = e.detail.value
        } else if (e.currentTarget.dataset.id == '9') {
            this.data.LNDEN = e.detail.value
        } else if (e.currentTarget.dataset.id == '11') {
            this.data.LWEIGHT = e.detail.value
        } else if (e.currentTarget.dataset.id == '12') {
            this.data.LCURRENCYID = e.detail.value
        } else if (e.currentTarget.dataset.id == '13') {
            this.data.LREMARK = e.detail.value
        } else if (e.currentTarget.dataset.id == '14') {
            this.data.LUNIT = e.detail.value
        } else if (e.currentTarget.dataset.id == '15') {
            this.data.LCOLORNAME = e.detail.value
        } else if (e.currentTarget.dataset.id == '16') {
            this.data.LCOLORCODE = e.detail.value
        } else if (e.currentTarget.dataset.id == '17') {
            this.data.LBATCH = e.detail.value
        } else if (e.currentTarget.dataset.id == '18') {
            this.data.LSAP = e.detail.value
        }
    },
    shanchu(e) { //删除从表信息
        let list = this.data.my_table
        console.log(e.currentTarget.dataset.index)
        list.splice(e.currentTarget.dataset.index, 1)
        this.setData({
            my_table: list,
        })
        wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000
        })
    },
    /**
     * @param {保存}
     */
    scan(e) {
        let that = this
        let table = [{
            'LTICKETNO': that.data.LTICKETNO, //编号
            "LSAP": that.data.LSAP,
            "LTICKETDATE": that.data.dateright, //日期
            'LREQDATE': that.data.LREQDATE, //要求交期
            "LPTNCODE": that.data.LPTNCODE, //客户
            "LCOMPANYID": that.data.LCOMPANYID, //签订公司
            "LSALEOPID": that.data.LSALEOPID, //业务员
            'LITEMCODE': that.data.LITEMCODE, // 产品编号
            'LITEMNAME': that.data.LITEMNAME, //产品名称
            'LNIANDU': that.data.LNIANDU, //捻度
            'LNDEN': that.data.LNDEN, //旦数
            'LITEMSTD': that.data.LITEMSTD, // 规格
            'LITEMCOMPOS': that.data.LITEMCOMPOS, // 成份
            // 'LWEIGHT': that.data.LWEIGHT, //数量
            'LNUMBER': that.data.LNUMBER,
            'LCOLORNAME': that.data.LCOLORNAME,
            'LCOLORCODE': that.data.LCOLORCODE,
            'LBATCH': that.data.LBATCH,
            "LUNIT": that.data.LUNIT, //计量方式
            'LCURRENCYID': that.data.LCURRENCYID, // 币种
            "LMAKEOPID": app.globalData.lusercode, // 制单人
            "LMAKEDATE": that.data.dateright, //制单日期
            "LREMARK": that.data.LREMARK, //备注
        }]
        let bb = {
            'table': table,
            "dts": that.data.my_table
        }
        if (that.data.LSAP == '') {
            wx.showModal({
                title: '提示',
                content: '请输入SAP单号',
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    if (res.confirm) {

                    }
                }
            })
        } else {
            wx.request({
                url: 'https://' + app.globalData.url + '/Contract/addContractAll',
                data: {
                    "model": JSON.stringify(bb),
                },
                method: 'POST',
                dataType: 'json',
                responseType: 'text',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                // 访问成功
                success: function (res) {
                    if (res.statusCode != '200') {
                        // console.log('statuscode:', res.statusCode)
                        wx.showModal({
                            title: '提示',
                            content: '添加失败',
                            showCancel: false,
                            confirmText: '确定',
                            success(res) {
                                if (res.confirm) {
                                    console.log('用户点击确定')

                                }
                            }
                        })
                    } else {

                        wx.navigateBack({
                            delta: 1
                        })
                        wx.showToast({
                            title: '成功',
                            icon: 'success',
                            duration: 2000
                        })
                    }

                }
            })
        }

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        app.globalData.my_table = ''
        let that = this
        var date = new Date()
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        var day = date.getDate()
        var lw = new Date(date - 1000 * 60 * 60 * 24 * 30) //最后一个数字30可改，30天的意思

        var date1 = new Date();
        var date3 = new Date(date1);
        date3.setDate(date1.getDate() + 30);
        var enddate = (date3.getFullYear() + "-" + ((date3.getMonth() + 1) < 10 ? '0' + (date3.getMonth() + 1) : (date3.getMonth() + 1)) + "-" + (date3.getDate() < 10 ? '0' + date3.getDate() : date3.getDate()))
        var lastY = lw.getFullYear()
        var lastM = lw.getMonth() + 1
        var lastD = lw.getDate()
        var startdate = lastY + '-' + (lastM < 10 ? '0' + lastM : lastM) + '-' + (lastD < 10 ? '0' + lastD : lastD) //三十天之前日期
        if (month < 10) {
            month = '0' + month
        }
        if (day < 10) {
            day = '0' + day
        }
        var dateright = year + '-' + month + '-' + day

        that.setData({
            dateright: dateright,
            LREQDATE: dateright,
            LMAKEOPID: app.globalData.lusername
        })
        that.setData({
            dateleft: startdate
        })
        wx.request({
            url: 'https://' + app.globalData.url + '/machineInfo/getCode',
            data: {
                RuleId: 112
            },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            header: {
                'content-type': 'application/json;charset=utf-8'
            },
            success: function (res) {
                if (res.data.success == true) {
                    that.setData({
                        LTICKETNO: res.data.obj
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '获取单号失败',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {

                            }
                        }
                    })
                }
            },
            fail: function (res) {
                that.setData({
                    loadModal: false
                })
                wx.showModal({
                    title: '提示',
                    content: '获取单号失败',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            },
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let that = this
        if (app.globalData.my_table != '') {
            that.data.my_table.push(app.globalData.my_table)
            that.setData({
                my_table: that.data.my_table
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})